/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelWordTable.java
 *
 * Created on Dec 5, 2011, 9:11:35 AM
 */
package management;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;

import target.*;
import xml.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author buer
 */
public class JPanelWordTable extends javax.swing.JPanel implements TableModelListener {

    /** Creates new form JPanelWordTable */
    public JPanelWordTable() {
        initComponents();
        createWordList(0);
    }

    public void createWordList(int searchRate) {
        WordSearching newWS = new WordSearching();
        int i, j;
        wordList = new ArrayList<Word>();
        if (searchRate < 5) {
            wordList = newWS.wordSearching("", String.valueOf(searchRate + 1), String.valueOf(searchRate + 1), "", "");
        } else {
            wordList = newWS.wordSearching("", "", "", "", ""); //读取全部单词     
        }

        final Object[] columnNames = {"单词", "评级", "记忆状态", "背诵历史"};
        Object[][] rowData = new Object[wordList.size()][4];

        //wordList内容存入rowData
        for (i = 0; i < wordList.size(); i++) {
            for (j = 0; j < 4; j++) {
                switch (j) {
                    case 0:
                        rowData[i][j] = wordList.get(i).word;
                        break;
                    case 1:
                        rowData[i][j] = wordList.get(i).rating;
                        break;
                    case 2:
                        if (wordList.get(i).status.compareTo(Word.STATUS.REM) == 0) {
                            rowData[i][j] = "记住了";
                        }
                        ;
                        if (wordList.get(i).status.compareTo(Word.STATUS.NREM) == 0) {
                            rowData[i][j] = "没记住";
                        }
                        ;
                        if (wordList.get(i).status.compareTo(Word.STATUS.DUMP) == 0) {
                            rowData[i][j] = "不记了";
                        }
                        ;
                        break;
                    case 3:
                        rowData[i][j] = wordList.get(i).date;
                        break;
                }
            }

        }
        //定义类
        myTableModel = new DefaultTableModel(rowData, columnNames) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if (columnIndex == 0 || columnIndex == 3) {
                    return false;
                } else {
                    return true;
                }
            }
        };

        //新建表格,加入表格事件监听器
        myTableModel.addTableModelListener(this);
        wordContent = new JTable(myTableModel);
        //加入下拉列表
        addNewCombobox();

        //设置表格属性
        setTableAttribute();

        //表格加入容器
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        int tableWidth=620, tableHeight=280;
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wordScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, tableWidth, Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wordScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, tableHeight, Short.MAX_VALUE).addContainerGap()));

    }

    private void addNewCombobox() {
        //加入下拉列表
        comboBox = new JComboBox();
        comboBox.addItem("1");
        comboBox.addItem("2");
        comboBox.addItem("3");
        comboBox.addItem("4");
        comboBox.addItem("5");
        comboBox2 = new JComboBox();
        comboBox2.addItem("记住了");
        comboBox2.addItem("没记住");
        comboBox2.addItem("不记了");
    }

    private void setTableAttribute() {
        //wordContent.setPreferredScrollableViewportSize(new Dimension(600, 100));//设置表格的大小
        wordContent.setRowHeight(30);//设置每行的高度为30
        wordContent.setRowHeight(0, 20);//设置第1行的高度为20
        wordContent.setRowMargin(5);//设置相邻两行单元格的距离
        wordContent.setRowSelectionAllowed(true);//设置可否被选择.默认为false
        wordContent.setSelectionBackground(Color.white);//设置所选择行的背景色
        wordContent.setSelectionForeground(Color.red);//设置所选择行的前景色
        wordContent.setGridColor(Color.black);//设置网格线的颜色

        //新建滚动窗体，将表格加入该滚动窗体并初始化
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        wordScrollPane.setViewportView(wordContent);
        wordScrollPane.setVerticalScrollBarPolicy(v);
        wordScrollPane.setHorizontalScrollBarPolicy(h);
        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        //TableColumn主要作用可以对表格中的某一列的属性进行编辑  
        TableColumn tableColumn = wordContent.getColumn("评级");
        TableColumn tableColumn2 = wordContent.getColumn("记忆状态");

        //利用TableColumn类中的setCellEditor()方法来设置单元格的编辑器  
        //DefaultCellEditor类可以将表格中的单元格设置成下拉框  
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
        tableColumn2.setCellEditor(new DefaultCellEditor(comboBox2));
    }

    //保存单词表
    public void saveWordTable() {
        int i;
        WordWriting changeWord = new WordWriting();
        if (Flag) {
            for (i = 0; i < rowPosList.size(); i++) {
                changeWord.wordWriting(wordList.get(Integer.valueOf(rowPosList.get(i).toString())));
            }
        }
        Flag=false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 486, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 81, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel myTableModel;
    private JTable wordContent;
    public JScrollPane wordScrollPane = new JScrollPane();
    private List<Word> wordList;
    private JComboBox comboBox, comboBox2;
    private ArrayList rowPosList = new ArrayList();
    private boolean Flag = false;

    @Override
    public void tableChanged(TableModelEvent e) {
        //在单词表中修改相应的单词内容
        int i, rowPos;
        String s;
        //产生修改标记。
        Flag = true;
        rowPos = e.getFirstRow();
        wordList.get(rowPos).rating = Integer.valueOf(wordContent.getValueAt(rowPos, 1).toString());
        s = (String) wordContent.getValueAt(rowPos, 2);
        System.out.println(wordList.size());
        if (s.compareTo("不记了") == 0) {
            wordList.get(rowPos).status = Word.STATUS.DUMP;
        }
        if (s.compareTo("记住了") == 0) {
            wordList.get(rowPos).status = Word.STATUS.REM;
        }
        if (s.compareTo("没记住") == 0) {
            wordList.get(rowPos).status = Word.STATUS.NREM;
        }

        //将行号加入行号列表
        rowPosList.add(rowPos);
    }
}
